package com.two.shopping.controller;

import com.two.shopping.pojo.Result;
import com.two.shopping.pojo.ShoppingCart;
import com.two.shopping.pojo.User;
import com.two.shopping.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//这是购物车的Controller
@Slf4j
@RequestMapping("/cart")
@RestController
public class ShoppingCartController {
    @Autowired
    IShoppingCartService shoppingCartService;
    @CrossOrigin
    //将商品添加至购物车
    @PostMapping("/add")
    public Result addCart(@RequestBody ShoppingCart shoppingCart) {
        //输出至控制台看一下是否成功传参
        log.info("将商品添加至购物车,{},{},{}", shoppingCart.getUserId(), shoppingCart.getCommodityId(), shoppingCart.getQuantity());
        try {
            shoppingCartService.addCart(shoppingCart);
        }
      catch (Exception e)
      {
          return Result.error("商品不够了");
      }
        return Result.success("添加成功");
    }
    @CrossOrigin
    //查询购物车的商品，参数为用户的id
    @GetMapping("/add")
    public Result findAll(Integer id) {
        User user = new User();
        user.setId(id);
        log.info("查询购物车里的商品,参数是用户的id,{}", user.getId());
        List<ShoppingCart> shoppingCartList = shoppingCartService.findAll(user);
        return Result.success(shoppingCartList);
    }
    @CrossOrigin
    //根据id删除购物车内容
    @PostMapping("/delete")
    public Result deleteCartId(@RequestBody ShoppingCart shoppingCart) {
        log.info("删除id为{}的购物车商品的信息", shoppingCart.getId());
        shoppingCartService.deleteCartId(shoppingCart);
        return Result.success("删除成功");
    }
    @CrossOrigin
    //结算购物车中id为{}的信息
    @PostMapping("/ok")
    public Result shoppingCartIdOkOrders(@RequestBody ShoppingCart shoppingCart) {
        log.info("结算购物车中id为{}的信息", shoppingCart.getId());
        shoppingCartService.shoppingCartIdOkOrders(shoppingCart);
        return Result.success("结算成功");
    }


}
